package com.qf.zuulserver11000.fallback;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * Zuul的熔断降级实现
 * @author q
 * @version V1.0
 * @date 2022/01/19
 **/
@Component
public class ZuulFallback implements FallbackProvider {

    /**
     * 需要熔断的服务，写服务名，如果所有的服务都需要熔断，那么就是null
     * @return
     */
    @Override
    public String getRoute() {
        //return "device-consumer";
        return null;
    }

    /**
     * 降级实现
     * @param route
     * @param cause
     * @return
     */
    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        return new ClientHttpResponse() {

            @Override
            public HttpStatus getStatusCode() throws IOException {
                //响应状态对象
                return HttpStatus.OK;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                //响应状态码
                return HttpStatus.OK.value();
            }

            @Override
            public String getStatusText() throws IOException {
                //响应状态文本
                return HttpStatus.OK.getReasonPhrase();
            }

            @Override
            public void close() {
                //不改动
            }

            @Override
            public InputStream getBody() throws IOException {
                //响应体对应的流
                /*ResultVO<String> vo = ResultVO.ok("熔断成功", "zuul降级");
                ObjectMapper objectMapper = new ObjectMapper();
                String json = objectMapper.writeValueAsString(vo);
                return new ByteArrayInputStream(json.getBytes());*/
                //return new ByteArrayInputStream("熔断成功，zuul降级".getBytes());
                /*
                code
                message
                data
                 */
                Map<String,Object> map = new HashMap<>();
                map.put("code",1000);
                map.put("message","fallback");
                map.put("data",null);
                //map-->json
                ObjectMapper objectMapper = new ObjectMapper();
                String json = objectMapper.writeValueAsString(map);
                //把字符串写进输入流
                ByteArrayInputStream inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
                return inputStream;
            }

            @Override
            public HttpHeaders getHeaders() {
                //响应头
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                return httpHeaders;
                //return null; -> 这样会报500
            }
        };
    }
}
